package com.leetcode.practice.self.ddd;

import com.leetcode.practice.self.entity.RoleDomain;
import com.leetcode.practice.self.entity.UserDomain;
import com.leetcode.practice.self.listen.BaseEvent;
import com.leetcode.practice.self.listen.ListenerPublisher;
import com.leetcode.practice.self.listen.RoleEvent;
import com.leetcode.practice.self.listen.UserEvent;
import com.leetcode.practice.self.mapper.TestMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
@Slf4j
public class TestRoleService {

    @Autowired
    private ListenerPublisher listenerPublisher;

    @SneakyThrows
    @EventListener
    @Async
    public Long addTestRole(UserEvent userDomainBaseEvent) {
        Thread.sleep(3000);
        log.info("设置角色");
        UserDomain data = userDomainBaseEvent.getData();
        RoleDomain roleDomain = new RoleDomain(data.getUserId(),data.getName());
        RoleEvent baseEvent = new RoleEvent(UUID.randomUUID().toString(), LocalDateTime.now(),roleDomain);
        listenerPublisher.pushBaseEvent(baseEvent);
        return roleDomain.getUserId();
    }
}
